<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Edit;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Role extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\Role::class,
            'validate' => \app\admin\validate\Role::class
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['title', '角色名', null, fn(Edit $e) => $e->require()],
                ['explain', '角色说明', null, fn(Edit $e) => $e->text()],
                ['status', '状态', fn(Column $col) => $col->switch('开启|关闭', '1|0')],
                ['create_time', '添加时间', fn(Column $col) => $col->width(150)],
                ['update_time', '更新时间', fn(Column $col) => $col->width(150)]
            ])
            ->addTopButtons(['add'])
            ->addRightButtons([
                'give' => fn(Button $btn) => $btn->title('分配权限')
                    ->url(url('give')->build())
                    ->event('od'),
                'edit',
                'delete'
            ], 220)
            ->addSearch([
                ['title', '角色名', fn(Search $search) => $search->condition('Like')]
            ])
            ->specifyQuery([['id', '>', 1]])
            ->fetch();
    }

    public function delete(): Response
    {
        $param = $this->request->post();

        return Result::fastReturn($this->model::_delete($param, $this->admin));
    }

    public function give(): Response|string
    {
        if ($this->request->isPost()) {
            try {
                $param = $this->request->post();
                $param['rules'] = $param['rules'] ? implode(',', $param['rules']) : '';
                $this->model::update($param);
                return Result::success('操作成功');
            } catch (\Exception $e) {
                return Result::fail($e->getMessage());
            }
        }
        extract((new \app\common\model\Menu())->getAuth($this->request->get('id')));
        function childrenFun($item, $rule)
        {
            $children = [];
            foreach ($item['child'] as $child) {
                $children[] = [
                    'id' => $child['id'],
                    'title' => $child['title'],
                    'spread' => true,
                    'children' => childrenFun($child, $rule),
                    'checked' => in_array($child['id'], $rule)
                ];
            }
            return $children;
        }

        $list = [];
        foreach ($menu as $item) {
            $data = [
                'id' => $item['id'],
                'title' => $item['title'],
                'spread' => true,
                'children' => childrenFun($item, $rule),
            ];
            $list[] = $data;
        }

        return View::assign('list', $list)->fetch();
    }
}